<template>
  <div></div>
</template>

<script>
import dayjs from 'dayjs'

export default {
  data() {
    return {
      tableRef: 'tableRef',
      dataSource: [],
      form: {},
      total: 0,
      tableEvent: {
        action: this.handleAction,
        fetchTable: this.fetchTable,
      },
      loading: false,
      apiName: 'orderList',
      formKey: this.$quinn.guid(),
    }
  },
  computed: {
    expFileName() {
      return '文件导出'
    },
    columns() {
      const name = this.$options.name
      console.log('columns------', name)
      return this.$columns[name] || []
    },
    tableOpts() {
      const {
        tableRef = 'tableRef',
        formKey,
        form,
        dataSource,
        columns,
        total,
        loading,
      } = this
      return {
        loading,
        ref: tableRef,
        key: formKey,
        form,
        total,
        dataSource,
        columns,
      }
    },
  },
  watch: {
    '$store.state.directoryMap': function () {
      if (this.form.directoryId) {
        this.form.directoryId.items = this.getDirectoryItem()
        this.formKey = this.$quinn.guid()
      }
    },
  },
  methods: {
    expTable(getAll = false) {
      let {dataSource: data, columns, expFileName: fileName} = this
      this.$quinn.expTable({
        fileName,
        data: data.map((e, i) => {
          e.sortIndex = i + 1
          return e
        }),
        columns,
      })
    },
    formArea() {
      const items = [];
      this.$api.areaLedgerAreaList().then(res => {
        sessionStorage.setItem('areaList', JSON.stringify(res))
      })
      const AREALIST = JSON.parse(sessionStorage.getItem('areaList'))
      for (const key in AREALIST) {
        items.push({
          label: AREALIST[key].areaName,
          value: AREALIST[key].area
        })
      }
      return {
        tag: "el-select",
        label: "区域",
        value: '',
        items,
      };
    },
    formDirectoryId() {
      return {
        tag: 'el-select',
        label: '套件',
        value: '',
        items: this.getDirectoryItem(),
        props: {
          clearable: true,
          filterable: true,
        },
      }
    },
    getDirectoryItem() {
      const selectMap = this.$store.state.directoryMap
      return Object.keys(selectMap).map((value) => ({
        value,
        label: selectMap[value],
      }))
    },
    fetchCurrentTable() {
      const {tableRef} = this
      const ref = this.$refs[tableRef]
      ref && ref.fetchTable()
    },
    handleAction(opts, row) {
    },
    formatQuery(opts) {
      const {date = [], ...params} = opts
      if (date && date[0] && date[1]) {
        params.startTime = dayjs(date[0]).format('YYYY-MM-DD HH:mm:ss')
        params.endTime = dayjs(date[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      return params
    },
    formatProduct(res) {
      this.total = res?.total || 0
      this.dataSource = (res?.data || []).map((e) => {
        if (e.updatedAt)
          e.updatedTime = dayjs(e.updatedAt).format('YYYY-MM-DD HH:mm:ss')
        if (e.createdAt) {
          e.createdTime = dayjs(e.createdAt).format('YYYY-MM-DD HH:mm:ss')
        }
        for (const k in e.directory || {}) {
          if (Object.hasOwnProperty.call(e.directory, k)) {
            e[`d_${k}`] = e.directory[k]
          }
        }
        e.directoryId = e.directory?.id
        return e
      })
    },
    formatData(res) {
      this.total = res?.total || 0
      this.dataSource = (res?.data || res || []).map((e) => {
        if (e.updatedAt)
          e.updatedTime = dayjs(e.updatedAt).format('YYYY-MM-DD HH:mm:ss')
        if (e.createdAt) {
          e.createdTime = dayjs(e.createdAt).format('YYYY-MM-DD HH:mm:ss')
        }
        return e
      })
    },
    fetchTable(opts) {
      this.formArea()
      const {apiName} = this
      this.loading = true
      this.$api[apiName](this.formatQuery(opts))
          .then(this.formatData)
          .finally((e) => {
            this.loading = false
          })
    },
    showDialog() {
      const ref = this.$refs.dialogRef
      ref && ref.showDialog(...arguments)
    },
    showConfirm(text = '确认删除？', path = '', params = {}, fn = () => {
    }) {
      this.$confirm(text).then((res) => {
        this.$api[path](params).then(this.fetchCurrentTable).finally(fn)
      })
    },
  },
}
</script>

<style lang="less" scoped></style>
